package com.CityHunter.Fetcher;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import com.CityHunter.Friend;
import com.CityHunter.Prey;

public class PreyFetcher {
	
	public void retriveFriendListByGroup(String groupTitle,String name) {
		String result;
	List<Friend> friendList= new ArrayList<Friend>();
		try {
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("grouptitle", groupTitle));
			nameValuePairs.add(new BasicNameValuePair("name", name));
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
			"http://tamama.slyrc.org/FYP/FYP/getGROUP_MEMBER.php");
	httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
			JSONArray json = new JSONArray(result);

			for (int i = 0; i < json.length(); i++) {
				friendList.add(new Friend(json.getJSONObject(i).getString("name"),
						Double.valueOf(json.getJSONObject(i).getString(
								"lastLongitude")), Double
								.valueOf(json.getJSONObject(i).getString(
										"lastLatitude"))));
				
				

			}
			

		} catch (Exception e) {
			friendList.add(new Friend("Wrong", 114.108589, 22.339865));
		}
	

	}
}
